package com.dq.Week3.day11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class MyCollection<E> implements Collection<E> {

    private Object[] data = new Object[10];

    private int count =0;


    @Override
    public int size() {
        return count;
    }

    @Override
    public boolean isEmpty() {
        return count==0;
    }

    @Override
    public boolean contains(Object o) {
        for (int i = 0; i < count; i++) {
            if(data[i].equals(o))
            {
                return true;
            }
        }
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            private int index = 0;
            @Override
            public boolean hasNext() {
                return index<count;
            }

            @Override
            public E next() {
                return (E)data[index++];
            }
        };
    }

    @Override
    public Object[] toArray() {

        return  Arrays.copyOf(data,count);
    }

    @Override
    public <T> T[] toArray(T[] a) {
        System.arraycopy(data,0,a,0,count);
        return a;
    }

    @Override
    public boolean add(E e) {

        data[count++] = e;
        if(data.length==count)
        {
            data = Arrays.copyOf(data,data.length*2);
        }

        return true;
    }

    @Override
    public boolean remove(Object o) {
        int index = getElementIndex(o);
        if(index==-1)
        {
            return false;
        }else{
            System.arraycopy(data,index+1,data,index,(count-(index+1)));
            data[count-1] = null;
            count--;
        }
       return false;
    }

    private int getElementIndex(Object o) {

        for (int i = 0; i < count; i++) {
            if(data[i].equals(o))
            {
                return i;
            }
        }
        return -1;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        for (Object o : c) {
            if(!contains(o))
            {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {

        for (E e : c) {
            add(e);
        }
        return true;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        boolean flag = false;
        for (Object o : c) {
            if (remove(o)) {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        //1.遍历当前集合，把应该删除的元素放到一个集合中
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            if (!c.contains(data[i])) {
                //添加到删除集合
                list.add(data[i]);
            }
        }

        //2.删除上得到集合中的所有元素

     return  removeAll(list);
    }

    @Override
    public void clear() {
        //底层数组中的元素清除掉
        data = new Object[10];
        //把count设置为0
        count=0;
    }

    @Override
    public String toString(){
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("[");
        for (int i = 0; i < count; i++) {
            stringBuffer.append(data[i]+" ");
        }
        stringBuffer.append("]");
        return stringBuffer.toString();

    }
}
